﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Solidi
{
	public class AutoDependencyResolver : DependencyResolverDecorator
	{
		public AutoDependencyResolver(IDependencyResolver resolver)
			: base(resolver)
		{
			
		}

		public override object Resolve(IDependencyContext context, IDependencyKey key)
		{
			if (key == null)
			{
				throw new ArgumentNullException("key");
			}

			object instance = base.Resolve(context, key);

			if (instance == null)
			{
				if (String.IsNullOrEmpty(key.Name))
				{
					//PERF: create light weight IDependency implementation
					DependencyRegistration registration = new DependencyRegistration(key, new DependencyTypeComposer(key.Type));

					return context.Lifetime.Resolve(context, new Dependency(context.Registry, registration));
				}
			}

			return instance;
		}
	}
}
